import { defineConfig, loadEnv } from 'vite';
import path, { resolve } from 'path';
import vue from '@vitejs/plugin-vue';
import AutoImport from 'unplugin-auto-import/vite';
import Components from 'unplugin-vue-components/vite';
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers';
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons';
import VueSetupExtend from 'vite-plugin-vue-setup-extend';
// const __filenameNew = fileURLToPath(import.meta.url)
// const __dirnameNew = path.dirname(__filenameNew)
var alias = {
    '@': resolve(__dirname, '.', './src/')
};
export default defineConfig(function (_a) {
    var command = _a.command, mode = _a.mode;
    //检查process.cwd()路径下.env.development.local、.env.development、.env.local、.env这四个环境文件
    var env = loadEnv(mode, process.cwd());
    //返回vite配置
    return {
        define: {
            __VUE_PROD_HYDRATION_MISMATCH_DETAILS__: false
        },
        server: {
            https: false,
            open: true,
            port: 7878,
            // cors: false, //为开发服务器配置 CORS
            //  hmr: true, //开启热更新
            host: "0.0.0.0",
            proxy: {
                '/api': {
                    target: env.VITE_BASE_URL,
                    changeOrigin: true,
                    //rewrite: (path) => path.replace(/^\/api/, '')
                    rewrite: function (path) { return path; }
                }
            }
        },
        build: {
            outDir: "dist",
            assetsDir: "assets",
            minify: "terser",
            sourcemap: false,
            terserOptions: {
                compress: {
                    drop_console: true,
                    drop_debugger: true
                }
            }
        },
        plugins: [
            vue(),
            AutoImport({
                // 自动导入相关的Api
                imports: [
                    'vue',
                    'vue-router',
                    'pinia',
                ],
                // 生成auto-import.d.ts声明文件
                dts: 'src/auto-import.d.ts',
                // 解析Api
                resolvers: [
                    ElementPlusResolver()
                ]
            }),
            Components({
                // dirs 指定组件所在位置，默认为 src/components
                // 可以让我们使用自己定义组件的时候免去 import 的麻烦
                dirs: ['src/components/'],
                // 配置需要将哪些后缀类型的文件进行自动按需引入，'vue' 为默认值
                extensions: ['vue'],
                // 解析组件，这里以 Element Plus 为例
                resolvers: [
                    ElementPlusResolver()
                ],
                // 生成components.d.ts
                dts: true,
                // 遍历子目录
                deep: true
            }),
            createSvgIconsPlugin({
                // 指定需要缓存的图标文件夹
                iconDirs: [path.resolve(process.cwd(), 'src/assets/svg')],
                // 指定symbolId格式
                symbolId: 'icon-[dir]-[name]'
            }),
            VueSetupExtend()
        ],
        resolve: {
            alias: alias
        }
    };
});
